package org.cadad.commands;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.cadad.BookInstance;
import org.cadad.RequestOperation;
import org.cadad.models.Book;
import org.cadad.parsers.BookParser;

public class MonthAvailabilityCommand {

	private List<Book>	listBook;
	private Calendar	calendar;

	public MonthAvailabilityCommand() {
		listBook = new ArrayList<Book>();
	}

	public List<Book> getBook() {
		return listBook;
	}

	public void execute(String from, String to, String isResident) {

		calendar = Calendar.getInstance();
		int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH) + 1;
		int year = calendar.get(Calendar.YEAR);
		int max_days_of_month = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		int month_for_search = 1;

		BookInstance currentBook = BookInstance.getInstance();
		for (int iteratorMonth = 0; iteratorMonth < month_for_search; iteratorMonth++) {
			if (month == 13) {
				month = 1;
				year++;
			}
			day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
			for (; day_of_month <= max_days_of_month; day_of_month += 7) {
				day_of_month++;
				RequestOperation requestOperation = new RequestOperation();
				String responseGetBody = requestOperation.request(from, to, isResident, day_of_month, month, year);
				Book book = new Book();
				BookParser bookParser = new BookParser();
				book = bookParser.parse(responseGetBody);
				listBook.add(book);

				currentBook.addBook(book);

			}
		}

	}

}
